#version 130

uniform int radius;
uniform sampler2D tex;

in vec2 fragmentTexCoord;
out vec4 fragColor;

void main(void)
{
	if (radius < 2) {
		fragColor = texture(tex, fragmentTexCoord);
		return;
	}

	vec2 pixelSize = vec2(1, 1) / textureSize(tex, 0);
	vec2 offset = fragmentTexCoord / pixelSize;
	vec2 index = floor(offset / radius) * radius;

	vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
	for (int i = 0; i < radius; ++i)
		for (int j = 0; j < radius; ++j)
			color += texture(tex, pixelSize * (index + vec2(i, j)));

	color /= (radius * radius);
	fragColor = color;
	return;
}

